package com.mall.web.control.jiance;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mall.client.ps.PsService;
import com.mall.common.util.PageUtil;
import com.mall.pojo.PsItem;
import com.mall.pojo.PsItemExample;
import com.mall.query.Page;
import com.mall.web.util.HttpUtil;

@Controller
@RequestMapping("/ps/admin/")
public class PsManageController {
	@Autowired
	private PsService psService;
	
	
	@RequestMapping("index")
	public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		PsItemListMap(request, response, modelMap);
		return "ps/admin/index";
	}
	
	@RequestMapping("psItemListMore")
	public String psItemListMore(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		PsItemListMap(request, response, modelMap);
		return "ps/admin/psItemListMore";
	}
	
	private ModelMap PsItemListMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE_MALL);
		Page page = new Page();
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		PsItemExample example = new PsItemExample();
		example.setOrderByClause("id desc");
		example.setStart(page.getBegin());
		example.setLimit(page.getLength());
		List<PsItem> psItemList = this.psService.getPsItemListByExample(example);
		Integer count = this.psService.getPsItemCountByExample(example);
		
		modelMap.put("psItemList", psItemList);
		modelMap.put("pageNo", pageNo);
		modelMap.put("count", count);
		modelMap.put("pageCount", PageUtil.getPageNO(count, pageSize));
		return modelMap;
	}
	
	@RequestMapping("addAndModifyPsItem")
	public String addAndModifyPsItem(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Integer isModifypsItemId = HttpUtil.getIntegerValueFromRequest(request, "psItemId");
		PsItem  isModifyPsItem = null;
		if (isModifypsItemId != null && isModifypsItemId>0) {
			 isModifyPsItem =this.psService.getPsItemById(isModifypsItemId);
		}
		modelMap.put("isModifypsItemId", isModifypsItemId);
		modelMap.put("isModifyPsItem", isModifyPsItem);
		return "ps/admin/addAndModifyPsItem";
		
	}
	
	@RequestMapping("addAndModifyPsItemAction")
	public String addAndModifyPsItemAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		String title = HttpUtil.getStringValueFromRequest(request, "title");
		String spurl = HttpUtil.getStringValueFromRequest(request, "spurl");
		String spcode = HttpUtil.getStringValueFromRequest(request, "spcode");
		String rjurl = HttpUtil.getStringValueFromRequest(request, "rjurl");
		String rjcode = HttpUtil.getStringValueFromRequest(request, "rjcode");
		String imageCode = HttpUtil.getStringValueFromRequest(request, "imageCode");
		String videoCode = HttpUtil.getStringValueFromRequest(request, "videoCode");
		Integer type = HttpUtil.getIntegerValueFromRequest(request, "type");
		
		Integer isModifypsItemId = HttpUtil.getIntegerValueFromRequest(request, "psItemId");
		PsItem  psItem = null;
		boolean isModify =false;
		
		Integer result = null;
		if (isModifypsItemId != null && isModifypsItemId>0) {
			psItem =this.psService.getPsItemById(isModifypsItemId);
			if (psItem != null) {
				isModify = true;
			}
		}else{
			psItem = new PsItem();	
		}
		psItem.setTitle(title);
		psItem.setYunpanPass(spcode);
		psItem.setYunpanUrl(spurl);
		psItem.setRuanjianUrl(rjurl);
		psItem.setRuanjianPass(rjcode);
		psItem.setCode(imageCode);
		psItem.setVideoCode(videoCode);
		psItem.setType(type);
		if (isModify) {
			result = this.psService.modifyPsItem(psItem);
		}else{
			result = this.psService.insertPsItem(psItem);
		}
		if (result != null && result>0) {
			return "redirect:/ps/admin/index.htm";
		}
	
		return "redirect:/ps/video/error.htm";
	}
}
